package com.hjli.tool;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLTool {

	/**
	 * 获取xml根元素
	 * */
	public Element xmlRoot(String xmlPath) {
		SAXReader saxReader = new SAXReader();
		Document document;
		try {
			document = saxReader.read(new File(xmlPath));
			// 获取根元素
			Element root = document.getRootElement();
			return root;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取第一个子元素
	 * */
	public Element getChildElement(Element root,String name){
		 return root.element(name);
	}
	
	/**
	 * 获取xml子元素
	 * */
	public List<Element> xmlChild(Element root){
		List<Element> childList = root.elements();
		return childList;
	}
	
	/**
	 * 获取指定的子元素
	 * */
	public List<Element> xmlChild(Element root,String childName){
		List<Element> childList = root.elements(childName);
		return childList;
	}
}
